package com.white.loggerserver

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import com.white.loggerserver.bean.ClientIpBean
import com.white.loggerserver.core.LogServerManager
import com.white.loggerserver.core.UserListenCallback
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.properties.Delegates

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private var datas = mutableListOf<String>()
    private var adapter: LogRuntimeAdapter by Delegates.notNull()

    private var ips = mutableListOf<ClientIpBean>()
    private var ipAdapter: ClientIpAdapter by Delegates.notNull()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar?.let {
            it.title = "日志服务"
            it.setDisplayHomeAsUpEnabled(true)
        }

        LogServerManager.instance.init(this)

        configAdapter()
        LogServerManager.instance.registerCallback(mCallback)

        btnClear.setOnClickListener(this)

        window_switcher.isChecked = LogServerManager.instance.isShowing

        window_switcher.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                LogServerManager.instance.showLogWindow()
            } else {
                LogServerManager.instance.closeLogWindow()
            }
        }
    }

    private fun configAdapter() {

        ipAdapter = ClientIpAdapter()
        clientsRecycle.adapter = ipAdapter
        ipAdapter.setNewInstance(ips)

        adapter = LogRuntimeAdapter()
        loggerRecycle.adapter = adapter
        adapter.setNewInstance(datas)
    }

    private var mCallback = Callback()

    inner class Callback : UserListenCallback {
        override fun onClientOnline(ip: String) {
            val exist = findIp(ip)
            if (exist == null) {
                runOnUiThread {
                    ipAdapter.addData(ClientIpBean(ips.size, "log", ip))
                }
            }
        }

        override fun onClientOffline(ip: String) {
            val ret = ips.find { ip == it.ip }
            ret?.let {
                runOnUiThread {
                    ipAdapter.remove(it)
                }
            }
        }

        override fun onReceiveMsg(ip: String, type: Int, timestamp: Long, msg: String) {
            //            datas.add(msg)
//            adapter.addData(msg)
//            sb.append(msg)
//            logContent.text = sb.toString()
//            Log.e("s", msg)
//            loggerRecycle.scrollToPosition(datas.size-1)
        }
    }

    private fun findIp(ip: String): ClientIpBean? {
        return ips.find { ip == it.ip }
    }

    override fun onClick(v: View?) {

        when (v) {
            btnClear -> {
                datas.clear()
                adapter.notifyDataSetChanged()
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_setting, menu)

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.menu_set -> {
                SettingsActivity.active(this)
            }
            R.id.menu_file -> {
                LogRecordListActivity.active(this)
            }
            R.id.menu_exit ->{
                LogServerManager.instance.closeLogWindow()
                LogServerManager.instance.destroy()
                finish()
            }
            android.R.id.home->{
                finish()
            }
        }
        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 12 && resultCode == Activity.RESULT_OK){
            LogServerManager.instance.notifyStyleChanged()
        }
    }
}